#include "mqttclient.h"
#include <QHostAddress>
#include <QDebug>

MqttClient::MqttClient(QString ip, int portnum, QObject *parent) : QObject(parent)
{
    this->hostname=ip;
    this->port=portnum;
    QHostAddress address(ip);
    client=new QMQTT::Client(address,portnum);
    connect(client,&QMQTT::Client::connected,this,&MqttClient::mqttconnected);
    connect(client,&QMQTT::Client::disconnected,this,&MqttClient::mqttdisconnected);
    connect(client,&QMQTT::Client::error,this,&MqttClient::mqtterror);
    connect(client,&QMQTT::Client::received,this,&MqttClient::mqttreadyread);
    client->setClientId("loragw");
//    client->setUsername("admin");
//    client->setPassword("123456");
}
void MqttClient::start()
{
    client->connectToHost();
}
void MqttClient::mqttconnected()
{
    qDebug("连接成功！");
}
void MqttClient::mqttdisconnected()
{
    qDebug("连接失败！");
}
void MqttClient::mqtterror(const QMQTT::ClientError error)
{
    qDebug()<<error;
}
void MqttClient::mqttreadyread(const QMQTT::Message &msg)
{
    qDebug(msg.payload());
}
